﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Quản_Lý_Thư_Viện
{
    public partial class frmQuyDinh : Form
    {
        public frmQuyDinh()
        {
            InitializeComponent();
            
        }
        
        ThamsoInfo info = new ThamsoInfo();
        bool bInserted = false;
         
        public static int sosachmuontoida,tuoitoithieu,tuoitoida,sotienmoingay,tiennotoida;
                private void frmQuyDinh_Load(object sender, EventArgs e)
        {
            ThamsoControl ts = new ThamsoControl();
            dataGridView1.DataSource = ts.laydsthamso();
            
        }
         public static void laygiatriquydinh()
                {
                    ThamsoControl tscontrol1 = new ThamsoControl();
                    DataTable dtqd = new DataTable();
                    dtqd = tscontrol1.laydsthamso();
                    DataRow dr1 = dtqd.Rows[0];// row so sach muon toi da
                    sosachmuontoida = Convert.ToInt16(dr1["GIATRI"].ToString());
                    DataRow dr2 = dtqd.Rows[1];
                    tuoitoithieu = Convert.ToInt16(dr2["GIATRI"].ToString());
                    DataRow dr3 = dtqd.Rows[2];
                    tuoitoida = Convert.ToInt16(dr3["GIATRI"].ToString());
                    DataRow dr4 = dtqd.Rows[3];
                    sotienmoingay = Convert.ToInt16(dr4["GIATRI"].ToString());
                    DataRow dr5 = dtqd.Rows[4];
                    tiennotoida = Convert.ToInt32(dr5["GIATRI"].ToString());
                }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount > 0)
            {
                txtMaThamSo.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                txtTenThamSo.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                txtGiaTri.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            bInserted = false;
            ShowControl(true);
        }
        private void ShowControl(bool bShow)
        {
            txtMaThamSo.Enabled =  bInserted;
            txtTenThamSo.Enabled = bShow;
            txtGiaTri.Enabled = bShow;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ThamsoControl ts = new ThamsoControl();
            info.Mathamso = txtMaThamSo.Text;
            info.Trigia = Convert.ToInt16(txtGiaTri.Text);
            ts.updatethamso(info);
            dataGridView1.DataSource = ts.laydsthamso();
            ShowControl(false);
        }
        private void KiemTraNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                MessageBox.Show("Yêu Cầu Nhập Số");
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Escape))
            {
                this.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }      
    }
}
